// completertextedit.cpp
#include "completertextedit.h"

#include <QAbstractItemView>
#include <QCompleter>
#include <QGraphicsDropShadowEffect>
#include <QKeyEvent>
#include <QString>
#include <QTextCursor>

CompleterTextEdit::CompleterTextEdit(QWidget *parent) :
    QTextEdit(parent), m_completer(NULL) {




}


void CompleterTextEdit::setCompleter(QCompleter *completer) {
    QString g_scrollVerticalWidth8Gray =
    "QScrollBar:vertical{ width:8px; background-color:#e9f1f7;}"
    "QScrollBar::handle:vertical{ width:8px; background-color:#6eade0; border-radius:4px;}"
    "QScrollBar::handle:vertical:hover{background-color:#2b76af;}"
    "QScrollBar::handle:vertical:pressed{background-color:#1e659b;}";
    QString qsCompleterStyle =
    //设置：背景色、字体颜色、大小、字体风格
    "QAbstractItemView{background-color: rgb(47,79,79);border-radius: 10px;color:#40E0D0;font-size:16px;font-family:Microsoft YaHei UI;outline:0px;}"
    "QAbstractItemView::item:hover{background-color:#7FFF00;color: #0000FF;}"
    "QAbstractItemView::item:selected{background-color:#FF8C00;color: #0000FF;}";
//    QString qsListViewStyle =
//            "QListView { background-color: rgb(255, 255, 255);border-radius: 10px; }";
    completer->popup()->setStyleSheet(qsCompleterStyle + g_scrollVerticalWidth8Gray);
    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect2 = new QGraphicsDropShadowEffect(this);
    shadow_effect2->setOffset(0, 0);
    //阴影颜色
    shadow_effect2->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect2->setBlurRadius(22);
    completer->popup()->setGraphicsEffect(shadow_effect2);
    completer->setCurrentRow(0);
    m_completer = completer;
    if (m_completer) {
        m_completer->setWidget(this);
        connect(m_completer, SIGNAL(activated(QString)),
                this, SLOT(onCompleterActivated(QString)));
    }
}

void CompleterTextEdit::keyPressEvent(QKeyEvent *e) {
    if (m_completer) {
        if (m_completer->popup()->isVisible()) {
            switch(e->key()) {
            case Qt::Key_Escape:
            case Qt::Key_Enter:
//                e->ignore();
//                return;
            case Qt::Key_Return:
            case Qt::Key_Tab:
                if (m_completer && m_completer->popup() && m_completer->model()->rowCount() > 0) {
                    qDebug() << m_completer->popup()->currentIndex().data().toString();
                    qDebug() << m_completer->popup()->model()->index(0,0).data().toString();
                    if(m_completer->popup()->currentIndex().data().toString() == ""){
                        m_completer->popup()->setCurrentIndex(m_completer->model()->index(0, 0));
                        QString text = m_completer->currentCompletion();
                        onCompleterActivated(text);
                    }
//                    qDebug() << m_completer->popup()->currentIndex().data().toString();
//                    qDebug()<< text;
//                    qDebug()<< wordUnderCursor();
//                    QString completionPrefix = wordUnderCursor();

//                    qDebug() << m_completer->currentRow();
                    //return;
                }
//                qDebug() << this->toPlainText().indexOf(wordUnderCursor());
//                qDebug() << this->toPlainText();
//                qDebug() << wordUnderCursor();
                e->ignore();
                return;
            default:
                break;
            }
        }
        QTextEdit::keyPressEvent(e);
        QString completerPrefix = this->wordUnderCursor();
        m_completer->setCompletionPrefix(completerPrefix); // 通过设置QCompleter的前缀，来让Completer寻找关键词
        m_completer->complete();

    }
}

void CompleterTextEdit::onCompleterActivated(const QString &completion) {
    QString completionPrefix = wordUnderCursor(),
            shouldInertText = completion;
    QTextCursor cursor = this->textCursor();
    if (!completion.contains(completionPrefix)) {
   // delete the previously typed.
        cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, completionPrefix.size());
        cursor.clearSelection();
    } else { // 补全相应的字符
        shouldInertText = shouldInertText.replace(
            shouldInertText.indexOf(completionPrefix), completionPrefix.size(), "");
    }
    cursor.insertText(shouldInertText);
}

void CompleterTextEdit::test(QString text){

    QString completionPrefix = wordUnderCursor(),
            shouldInertText = text;
    QTextCursor cursor = this->textCursor();
    if (!text.contains(completionPrefix)) {
   // delete the previously typed.
        cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, completionPrefix.size());
        cursor.clearSelection();
    } else { // 补全相应的字符
        shouldInertText = shouldInertText.replace(
            shouldInertText.indexOf(completionPrefix), completionPrefix.size(), "");
    }
    cursor.insertText(shouldInertText);
}

QString CompleterTextEdit::wordUnderCursor() { //不断向左移动cursor，并选中字符，并查看选中的单词中是否含有空格——空格作为单词的分隔符
    QTextCursor cursor = this->textCursor();
    while (cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor)) {
        if (cursor.selectedText().contains(" ")) {
            break;
        }
    }
    return cursor.selectedText().remove(" ");
}
